package com.heliang.ui.info;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.appxiehui2.R;
import com.heliang.dataaccess.bean.AdvBean;
import com.heliang.dataaccess.bean.InfoBean;
import com.heliang.dataaccess.bean.MailListBean;

public class MyListView extends ListView {
	private GestureDetector mGestureDetector;
	View.OnTouchListener mGestureListener;
	private Context context;
	private Activity activity;
	private ViewPager viewPager;

	private ListView hotNews;
	private List<InfoBean> infoBeans = new ArrayList<InfoBean>();
	private List<MailListBean> members;

	private Gallery aliveMember;

	private int currentItem = 0;

	private ScheduledExecutorService scheduledExecutorService;

	private TextView advTitle;

	private List<View> dots;

	private List<AdvBean> advBeans = new ArrayList<AdvBean>();
	
	
	public void setViewPager(ViewPager viewPager) {
		this.viewPager = viewPager;
	}

	public MyListView(Context context,Activity activity) {
		super(context);
		this.context = context;
		this.activity = activity;
		//init(context);

		scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
		scheduledExecutorService.scheduleAtFixedRate(new ScrollTask(), 3, 10,
				TimeUnit.SECONDS);

	}

	public MyListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		mGestureDetector = new GestureDetector(new YScrollDetector());
		setFadingEdgeLength(0);
		// init(context);

		scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
		scheduledExecutorService.scheduleAtFixedRate(new ScrollTask(), 3, 10,
				TimeUnit.SECONDS);

	}

	public MyListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		super.onInterceptTouchEvent(ev);
		return mGestureDetector.onTouchEvent(ev);
	}

	class YScrollDetector extends SimpleOnGestureListener {
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			if (Math.abs(distanceY) >= Math.abs(distanceX)) {
				Log.e("jj", "上下....");
				return true;
			}
			Log.e("jj", "左右....");
			return false;
		}

//		@Override
//		public boolean onSingleTapUp(MotionEvent e) {
//			Toast.makeText(context, "图" + viewPager.getCurrentItem(), 1).show();
//			return super.onSingleTapUp(e);
//		}

	}

	public void init(Context context) {
		// BuildDate initDate = new BuildDate();

		// 导航广告以及文字介绍
		hotNews = (ListView) findViewById(R.id.news);

		// infoBeans = initDate.buildInfoBean();

		// 推荐咨询
		InfoListAdapter infoAdapter = new InfoListAdapter(activity, infoBeans,context);

		initFooter();

		initRecomandAdv();

		hotNews.setAdapter(infoAdapter);

		initAliveMember();

	}

	/**
	 * 初始化推荐咨询底部的数据
	 */
	public void initFooter() {
		LayoutInflater flater = LayoutInflater.from(context);
		View footerView = flater.inflate(R.layout.index_footer, null);
		aliveMember = (Gallery) footerView
				.findViewById(R.id.alive_member_people);
		hotNews.addFooterView(footerView);
	}

	/**
	 * 初始化活动会员
	 */
	public void initAliveMember() {
		System.out.println(aliveMember + "++++++++++++++++++++++++++");
		// BuildDate buildDate = new BuildDate();
		// members = buildDate.buidMember();
    
		AliveMemberAdapter aliveMemberAdapter = new AliveMemberAdapter(context,
				members);
		aliveMember.setAdapter(aliveMemberAdapter);

	}

	// 切换当前显示的图片
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			viewPager.setCurrentItem(currentItem);// 切换当前显示的图片
		};
	};

	/**
	 * 换行切换任务
	 * 
	 * @author Administrator
	 * 
	 */
	private class ScrollTask implements Runnable {
		public void run() {
			synchronized (viewPager) {
				currentItem = (currentItem + 1) % advBeans.size();
				handler.obtainMessage().sendToTarget(); // 通过Handler切换图片
			}
		}
	}

	/**
	 * 当viewPage改变时监听事件
	 * 
	 * @author Administrator
	 * 
	 */
	public class PageChangerLister implements OnPageChangeListener {
		private int oldPosition = 0;

		public void onPageScrollStateChanged(int arg0) {
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		public void onPageSelected(int position) {
			currentItem = position;
			advTitle.setText(advBeans.get(position).getDec());
			dots.get(oldPosition).setBackgroundResource(R.drawable.dot_normal);
			dots.get(position).setBackgroundResource(R.drawable.dot_focused);
			oldPosition = position;
		}

	}

	/**
	 * 分类选中后响应事件
	 */
	OnItemClickListener onItemClickListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			InfoCatListAdapter adapter = (InfoCatListAdapter) parent
					.getAdapter();
			adapter.setSelectedTab(position);
		}
	};

	/**
	 * 初始化四屏切换广告
	 */
	public void initRecomandAdv() {
		// BuildDate buildDate = new BuildDate();
		//advBeans = buildDate.BuildAdvDate();
		LayoutInflater layoutInflater = LayoutInflater.from(context);
		View headview = layoutInflater.inflate(R.layout.index_recomand_adv,
				null);
		FrameLayout framLayout = (FrameLayout) headview
				.findViewById(R.id.fl_main);
		// LayoutParams layoutParams = framLayout.getLayoutParams();
		// layoutParams.height = 180;
		// framLayout.setLayoutParams(layoutParams);

		viewPager = (ViewPager) headview.findViewById(R.id.vPager);
		advTitle = (TextView) headview.findViewById(R.id.tv_title);

		dots = new ArrayList<View>();
		dots.add(headview.findViewById(R.id.v_dot0));
		dots.add(headview.findViewById(R.id.v_dot1));
		dots.add(headview.findViewById(R.id.v_dot2));
		dots.add(headview.findViewById(R.id.v_dot3));
		dots.add(headview.findViewById(R.id.v_dot4));

		//viewPager.setAdapter(new AdvAdapter(context, advBeans));
		hotNews.addHeaderView(headview);

		viewPager.setOnPageChangeListener(new PageChangerLister());
	}
	
	

}